package cn.mhome.merchant.dao.jpa.clazz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.mhome.merchant.dao.common.SqlBuffer;
import cn.mhome.merchant.dao.common.po.ClassType;
import cn.mhome.merchant.dao.spring.data.repository.BaseJpaDaoSupport;
import cn.mhome.merchant.entity.clazz.param.ClassTypeParam;
import cn.mhome.merchant.entity.clazz.result.ClassTypeResult;
import cn.mhome.merchant.entity.common.result.Pager;
import cn.mhome.merchant.util.StringUtils;

public class ClassTypeDaoCustomImpl extends BaseJpaDaoSupport<ClassType,Long> implements ClassTypeDaoCustom{

	@Override
	public List<ClassTypeResult> findForList(ClassTypeParam classParam) {

		SqlBuffer sqlBuffer = new SqlBuffer("select ct.class_type_id,ct.class_type_name,ct.super_class_id,ct.business_num,ct.class_type_icon,ct.state, ct.index,ctp.class_type_name as super_class_name from class_type ct "
				+ " left join class_type ctp on ctp.class_type_id = ct.super_class_id "
				+ " left join sys_user su ON su.userid = ct.create_user"
				+ " where ct.super_class_id > 0 ");
		Map<String,Object> params = new HashMap<String,Object>();
		handlerParam(classParam,params,sqlBuffer);
		sqlBuffer.orderby("ct.create_time");
		return (List<ClassTypeResult>)super.findBySqlForList(sqlBuffer.toString(),params, ClassTypeResult.class);
	}

	@Override
	public Pager<ClassTypeResult> findForPage(ClassTypeParam classParam) {
		SqlBuffer selectSql = new SqlBuffer("select ct.class_type_id,ct.class_type_name,ct.super_class_id,ct.class_type_icon,ct.business_num,ct.state,ctp.class_type_name as super_class_name");
		SqlBuffer fromSql = new SqlBuffer(" from class_type ct "
				+ " left join class_type ctp on ctp.class_type_id = ct.super_class_id "
				+ " left join sys_user su ON su.userid = ct.create_user"
				+ " where ct.super_class_id > 0 ");
		Map<String,Object> params = new HashMap<String,Object>();
		handlerParam(classParam,params,fromSql);
		fromSql.orderby("ct.create_time");
		return (Pager<ClassTypeResult>)super.findBySqlForPage(selectSql.toString(), fromSql.toString(), params, ClassTypeResult.class, classParam.getPage(), classParam.getPageSize());
	}

	
	private void handlerParam(ClassTypeParam classParam ,Map<String, Object> params, SqlBuffer sqlBuffer) {
		if(classParam.getSuperClassId() > 0){
			sqlBuffer.append(" and ct.super_class_id =:superClassId");
			params.put("superClassId", classParam.getSuperClassId());
		}
		if(classParam.getClassTypeId() > 0){
			sqlBuffer.append(" and ct.class_type_id =:classTypeId");
			params.put("classTypeId", classParam.getClassTypeId());
		}
		if(!StringUtils.isBlank(classParam.getClassTypeName())){
			sqlBuffer.append(" and ct.class_type_name like:classTypeName");
			params.put("classTypeName","%"+classParam.getClassTypeName()+"%");
		}
		if(classParam.getState() > 0){
			sqlBuffer.append(" and ct.state =:state");
			params.put("state", classParam.getState());
		}
	}
}
